package edu.gatech.stolen.tea;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.util.Log;

/**
 * This class get the list of items from a username and edit them
 * serves as a data storage
 * @author Leshi
 * 
 */
public class ItemEditor implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3L;
	/**
	 * the storage of all the items that's created by every user
	 */
	public  Map<String, ArrayList<ToDoItem>> itemStorage = new HashMap<String, ArrayList<ToDoItem>>();
	/**
	 * the arraylist of item user holds
	 */
	private  ArrayList<ToDoItem> itemList;
	/**
	 * the current user
	 */
	public  User currentUser;
	/**
	 * the current selected item name in string
	 */
	public  String currentItemName;

	/**
	 * the itemEditor takes in a user, and extract his list of items from the
	 * storage
	 * 
	 * @param user
	 */
	public ItemEditor() {

	}

	/**
	 * sets the current user of this storage space
	 * 
	 * @param user
	 */
	public  void setCurrentUser(User user) {
		
		this.currentUser = user;
		this.itemList = itemStorage.get(user.getUser());
		//Log.d("currentUser", "itemStorage has"+itemStorage);
		//Log.d("currentUser", "current user set to "+currentUser.getUser() +"and has list"+ itemStorage.get(user));
	}

	/**
	 * adding a new user to the item storage
	 * 
	 * @param user
	 */
	public  void addNewUser(User user) {
		ArrayList<ToDoItem> newList = new ArrayList<ToDoItem>();
		itemStorage.put(user.getUser(), newList);
		//Log.d("ItemEditor", "new user is "+user.getUser()+ " has array " +itemStorage.get(user.getUser()));
	}

	/**
	 * add a new Item to the itemlist
	 * 
	 * @param item
	 * @return
	 */
	public  ArrayList<ToDoItem> addItem(ToDoItem item) {
		//Log.d("addnewItem", "itemlist is "+itemList+ "currentUser is "+currentUser.getUser());
		this.itemList.add(item);
		return itemList;
	}

	/**
	 * get the list of items current user holds
	 * 
	 * @return
	 */
	public  ArrayList<ToDoItem> getItemList() {
		return itemList;
	}

	/**
	 * get a single item by its name from the item list
	 * 
	 * @param name
	 * @return
	 * @throws NullPointerException
	 */
	public  ToDoItem getItem(String name){
		if(!itemList.isEmpty()){
		for (ToDoItem i : itemList) {
			if (i.getName().equals(name)) {
				return i;
			}
		}
		}
		return null;
	}

	/**
	 * remove an item by its name from the itemlist if the item doesn't exist, then nothing
	 * is done
	 * 
	 * @param name
	 */
	public  void deleteItem(String name) {
		if(!itemList.isEmpty()){
			for(ToDoItem i : itemList){
				if(i.getName().equals(name)){
					itemList.remove(i);
					Log.d("DELETE", "deleted item "+name);
				}
			}
		}
	}


	/**
	 * return a list of item names in string
	 * 
	 * @return list of string names
	 */
	public  ArrayList<String> getItemNames() {
		ArrayList<String> strList = new ArrayList<String>();
		
		if (!itemList.isEmpty()) {
			for (ToDoItem i : itemList) {
				strList.add(i.getName());
			}
		}
		return strList;
	}

}
